import { defineStore } from "pinia";
import { useUserStore } from "./user";
import axios from "axios";

export const useMessageStore = defineStore('message', () => {

    const init = async () => {
        try {
            const userStore = useUserStore();
            const userId = userStore.getId;
            if (userId != undefined) {
                await axios.post('/api/message/connect', { userId });
                console.log("Connected to message service");
            }
        } catch (error) {
            console.error("Failed to initialize message service:", error);
        }
    }

    const publish = async (topic: string) => {
        try {
            await axios.post('/api/message/publish', { topic });
        } catch (error) {
            console.error("Failed to publish message:", error);
        }
    }

    return {
        init, publish
    }
})